Skip to contentMethod: getValue(ELContext, Object, Object)
1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.el;
20:
21: import static java.lang.Boolean.TRUE;
22:
23: import java.beans.FeatureDescriptor;
24: import java.util.ArrayList;
25: import java.util.Enumeration;
26: import java.util.Iterator;
27: import java.util.List;
28: import java.util.MissingResourceException;
29: import java.util.ResourceBundle;
30:
31: /**
32: * Defines property resolution behavior on instances of {@link java.util.ResourceBundle}.
33: *
34: * <p>
35: * This resolver handles base objects of type <code>java.util.ResourceBundle</code>. It accepts any object as a property
36: * and coerces it to a <code>java.lang.String</code> for invoking
37: * {@link java.util.ResourceBundle#getObject(java.lang.String)}.
38: *
39: * <p>
40: * This resolver is read only and will throw a {@link PropertyNotWritableException} if <code>setValue</code> is called.
41: *
42: * <p>
43: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
44: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
45: *
46: * @see CompositeELResolver
47: * @see ELResolver
48: * @see java.util.ResourceBundle
49: *
50: * @since Jakarta Server Pages 2.1
51: */
52: public class ResourceBundleELResolver extends ELResolver {
53:
54: /**
55: * If the base object is an instance of <code>ResourceBundle</code>, the provided property will first be coerced to a
56: * <code>String</code>. The <code>Object</code> returned by <code>getObject</code> on the base
57: * <code>ResourceBundle</code> will be returned.
58: *
59: * <p>
60: * If the base is <code>ResourceBundle</code>, the <code>propertyResolved</code> property of the <code>ELContext</code>
61: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
62: * after this method is called, the caller should ignore the return value.
63: *
64: * @param context The context of this evaluation.
65: * @param base The ResourceBundle to analyze.
66: * @param property The name of the property to analyze. Will be coerced to a <code>String</code>.
67: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
68: * <code>null</code> if property is <code>null</code> otherwise the <code>Object</code> for the given key (property
69: * coerced to <code>String</code>) from the <code>ResourceBundle</code>. If no object for the given key can be found,
70: * then the <code>String</code> "???" + key + "???".
71: * @throws NullPointerException if context is <code>null</code>
72: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
73: * exception must be included as the cause property of this exception, if available.
74: */
75: @Override
76: public Object getValue(ELContext context, Object base, Object property) {
77:• if (context == null) {
78: throw new NullPointerException();
79: }
80:
81:• if (base instanceof ResourceBundle) {
82: context.setPropertyResolved(true);
83:• if (property != null) {
84: try {
85: return ((ResourceBundle) base).getObject(property.toString());
86: } catch (MissingResourceException e) {
87: return "???" + property + "???";
88: }
89: }
90: }
91:
92: return null;
93: }
94:
95: /**
96: * If the base object is an instance of <code>ResourceBundle</code>, return <code>null</code>, since the resolver is
97: * read only.
98: *
99: * <p>
100: * If the base is <code>ResourceBundle</code>, the <code>propertyResolved</code> property of the <code>ELContext</code>
101: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
102: * after this method is called, the caller should ignore the return value.
103: * </p>
104: *
105: * @param context The context of this evaluation.
106: * @param base The ResourceBundle to analyze.
107: * @param property The name of the property to analyze.
108: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
109: * <code>null</code> otherwise undefined.
110: * @throws NullPointerException if context is <code>null</code>
111: */
112: @Override
113: public Class<?> getType(ELContext context, Object base, Object property) {
114: if (context == null) {
115: throw new NullPointerException();
116: }
117:
118: if (base instanceof ResourceBundle) {
119: context.setPropertyResolved(true);
120: /*
121: * ResourceBundles are always read-only so fall-through to return
122: * null
123: */
124: }
125:
126: return null;
127: }
128:
129: /**
130: * If the base object is a ResourceBundle, throw a {@link PropertyNotWritableException}.
131: *
132: * @param context The context of this evaluation.
133: * @param base The ResourceBundle to be modified. Only bases that are of type ResourceBundle are handled.
134: * @param property The String property to use.
135: * @param value The value to be set.
136: * @throws NullPointerException if context is <code>null</code>.
137: * @throws PropertyNotWritableException Always thrown if base is an instance of ReasourceBundle.
138: */
139: @Override
140: public void setValue(ELContext context, Object base, Object property, Object value) {
141: if (context == null) {
142: throw new NullPointerException();
143: }
144:
145: if (base instanceof ResourceBundle) {
146: context.setPropertyResolved(true);
147: throw new PropertyNotWritableException("ResourceBundles are immutable");
148: }
149: }
150:
151: /**
152: * If the base object is not null and an <code>instanceof</code> {@link ResourceBundle}, return <code>true</code>.
153: *
154: * @param context The context of this evaluation.
155: * @param base The ResourceBundle to be modified. Only bases that are of type ResourceBundle are handled.
156: * @param property The String property to use.
157: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
158: * <code>true</code> otherwise undefined.
159: * @throws NullPointerException if context is <code>null</code>
160: */
161: @Override
162: public boolean isReadOnly(ELContext context, Object base, Object property) {
163: if (context == null) {
164: throw new NullPointerException();
165: }
166:
167: if (base instanceof ResourceBundle) {
168: context.setPropertyResolved(true);
169: return true;
170: }
171:
172: return false;
173: }
174:
175: /**
176: * If the base object is a ResourceBundle, returns an <code>Iterator</code> containing the set of keys available in the
177: * <code>ResourceBundle</code>. Otherwise, returns <code>null</code>.
178: *
179: * <p>
180: * The <code>Iterator</code> returned must contain zero or more instances of {@link java.beans.FeatureDescriptor}. Each
181: * info object contains information about a key in the ResourceBundle, and is initialized as follows:
182: * <ul>
183: * <li>displayName - The <code>String</code> key
184: * <li>name - Same as displayName property.</li>
185: * <li>shortDescription - Empty string</li>
186: * <li>expert - <code>false</code></li>
187: * <li>hidden - <code>false</code></li>
188: * <li>preferred - <code>true</code></li>
189: * </ul>
190: *
191: * In addition, the following named attributes must be set in the returned <code>FeatureDescriptor</code>s:
192: * <ul>
193: * <li>{@link ELResolver#TYPE} - <code>String.class</code></li>
194: * <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - <code>true</code></li>
195: * </ul>
196: *
197: *
198: * @param context The context of this evaluation.
199: * @param base The bundle whose keys are to be iterated over. Only bases of type <code>ResourceBundle</code> are handled
200: * by this resolver.
201: * @return An <code>Iterator</code> containing zero or more (possibly infinitely more) <code>FeatureDescriptor</code>
202: * objects, each representing a key in this bundle, or <code>null</code> if the base object is not a ResourceBundle.
203: *
204: * @deprecated This method will be removed without replacement in EL 6.0
205: */
206: @Deprecated(forRemoval = true, since = "5.0")
207: @Override
208: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
209: if (base instanceof ResourceBundle) {
210: ResourceBundle bundle = (ResourceBundle) base;
211: List<FeatureDescriptor> features = new ArrayList<>();
212: String key = null;
213: FeatureDescriptor desc = null;
214:
215: for (Enumeration<String> e = bundle.getKeys(); e.hasMoreElements();) {
216: key = e.nextElement();
217: desc = new FeatureDescriptor();
218: desc.setDisplayName(key);
219: desc.setExpert(false);
220: desc.setHidden(false);
221: desc.setName(key);
222: desc.setPreferred(true);
223: desc.setValue(TYPE, String.class);
224: desc.setValue(RESOLVABLE_AT_DESIGN_TIME, TRUE);
225: features.add(desc);
226: }
227:
228: return features.iterator();
229: }
230:
231: return null;
232: }
233:
234: /**
235: * If the base object is a ResourceBundle, returns the most general type that this resolver accepts for the
236: * <code>property</code> argument. Otherwise, returns <code>null</code>.
237: *
238: * <p>
239: * Assuming the base is a <code>ResourceBundle</code>, this method will always return <code>String.class</code>.
240: *
241: * @param context The context of this evaluation.
242: * @param base The bundle to analyze. Only bases of type <code>ResourceBundle</code> are handled by this resolver.
243: * @return <code>null</code> if base is not a <code>ResourceBundle</code> otherwise <code>String.class</code>.
244: */
245: @Override
246: public Class<?> getCommonPropertyType(ELContext context, Object base) {
247: if (base instanceof ResourceBundle) {
248: return String.class;
249: }
250:
251: return null;
252: }
253: }